package com.zbkj.front.netty.server;

import com.zbkj.common.constants.RedisConstants;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.service.service.SendMessageService;
import com.zbkj.service.websocket.ChannelQueueDTO;
import io.netty.channel.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class ChannelMessageQueueJob {

    private final RedisUtil redisUtil;
    private final SendMessageService sendMessageService;

    /**
     * 队列处理，发送消息
     */
    @Scheduled(fixedDelay = 2000)
    public void processChannelQueue() {
        while (true) {
            Object rightPop = redisUtil.getRightPop(RedisConstants.CHANNEL_MESSAGE_QUEUE_KEY, 1L);
            if (rightPop == null) {
                break;
            }

            ChannelQueueDTO channelQueueDTO = (ChannelQueueDTO) rightPop;
            Channel channel = YgxHandler.findChannelById(channelQueueDTO.getChannelId());
            sendMessageService.sendMessage(channelQueueDTO.getAcceptDataContent(), channel);
        }
    }
}
